package cn.holy.app.xcddt.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.domain.RecordBean
import cn.holy.app.xcddt.net.callback.LoadingCallback
import cn.holy.app.xcddt.repository.RecordRepository
import cn.holy.app.xcddt.utils.HDateUtils
import com.alibaba.android.arouter.launcher.ARouter
import com.orhanobut.logger.Logger
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout

import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.fragment_list.*

import javax.inject.Inject

/**
 * Created by 69568 on 2018/2/24.
 */

class ArchiveFragment : Fragment() {


    @Inject
    lateinit var recordRepository: RecordRepository
    private var mRecordBeans: ArrayList<RecordBean> = ArrayList()
    private var commonAdapter: CommonAdapter<RecordBean>? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_list, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        App.appComponent.inject(this)
        status = arguments!!.getInt("status")
        initData()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        Logger.d(" setUserVisibleHint() --> isVisibleToUser = " + isVisibleToUser)

        if (isVisibleToUser) {
            refreshLayout?.autoRefresh()
        }
        super.setUserVisibleHint(isVisibleToUser)
    }

    private fun initData() {
        val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        refreshLayout.setOnRefreshListener {
            page = 1
            loadArchives(page, status)
        }
        refreshLayout.setOnLoadMoreListener {
            page++
            loadArchives(page, status)
        }
        listview.layoutManager = layoutManager
        listview.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        commonAdapter = object : CommonAdapter<RecordBean>(context, R.layout.layout_archive_item, mRecordBeans) {

            override fun convert(holder: ViewHolder, recordBean: RecordBean, position: Int) {

                holder.getView<TextView>(R.id.tv_record_no).text = "案卷编号：" + recordBean.record_no
                holder.getView<TextView>(R.id.tv_message).text = recordBean.message
                holder.getView<TextView>(R.id.tv_address).text = recordBean.address
                holder.getView<TextView>(R.id.tv_status).text = recordBean.status_label
                holder.getView<TextView>(R.id.tv_record_people).text = recordBean.record_people
                holder.getView<TextView>(R.id.tv_record_group).text = "第" + (recordBean.record_num ) + "次巡查"

                holder.getView<TextView>(R.id.tv_time).text = "记录时间：" + HDateUtils.dateToStrLong(recordBean.record_date)
            }
        }
        commonAdapter?.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
            override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
                val item = mRecordBeans[position]
                ARouter.getInstance().build("/app/archives/detail").withSerializable("archive", item)
                        .navigation()
            }

            override fun onItemLongClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int): Boolean {
                return true
            }

        })
        listview.adapter = commonAdapter
        if (userVisibleHint) {
            refreshLayout.autoRefresh()
        }
    }

    private var page = 1
    private var status = 0
    private fun loadArchives(page: Int, status: Int) {
        recordRepository.loadArcives(page, status, object : LoadingCallback<List<RecordBean>> {
            override fun onStart() {
            }

            override fun onError(msg: String) {
            }

            override fun onSuccess(t: List<RecordBean>) {
                if (page == 1) {
                    mRecordBeans.clear()
                    refreshLayout.finishRefresh()
                } else {
                    refreshLayout.finishLoadMore()
                }
                mRecordBeans.addAll(t)
                commonAdapter?.notifyDataSetChanged()
            }

            override fun onSuccess() {
            }

            override fun onCompleted() {
            }

        })
    }
}